#include "popup.h"
#include "ui_popup.h"
#include <QtGui/QCloseEvent>
#include <QDesktopWidget>
#include <QDesktopServices>
#include <QMessageBox>
#include <QString>
#include <QWebFrame>
#include <QWebSettings>
#include <QtPlugin>
#include <QShortcut>
#include <hotkey.h>
#include <QxtGlobalShortcut>
#include <QDebug>

//Q_IMPORT_PLUGIN(qjpeg);
//Q_IMPORT_PLUGIN(qgif);

bool visible = false;

Popup::Popup(QWidget *parent) : QWidget(parent), ui(new Ui::Popup){
    ui->setupUi(this);
    QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
    QWebSettings::globalSettings()->setOfflineStoragePath(QString("/tmp/weboob"));
    setWebViewLocation("http://weboob.com/weboob-welcome/welcome.html");

    javascriptXSS = QString("");

    createActions();
    createTrayIcon();
    trayIcon->show();
    connectObject();
    connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(connectObject()));
}

Popup::~Popup(){
    delete ui;
}

void Popup::keypress(QString mykey){
    ui->webView->page()->mainFrame()->evaluateJavaScript("hotKeyRecieved('"+mykey+"');");
}

void Popup::registerHotKey(QString keyCombo){
    QxtGlobalShortcut *shortcut = new QxtGlobalShortcut(QKeySequence(keyCombo), this);
    HotKey *veryhot = new HotKey(keyCombo, this);
    connect(shortcut, SIGNAL(activated()), veryhot, SLOT(pressed()));
}

void Popup::setWebViewLocation(QString location){
    ui->webView->load(QUrl(location));
}

void Popup::changePopupSize(int width, int height, int atTheTop){
    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect clientRect = desktopWidget->availableGeometry();
    int x,y;

    if(clientRect.width() < width){
        width = clientRect.width();
    }
    if(clientRect.height() < height){
        height = clientRect.height();
    }

    ui->webView->setGeometry(0,0,width,height);

    switch(atTheTop) {
    case 0:
        x = clientRect.right() - width;
        y = clientRect.bottom() - height;
        break;
    case 1:
        x = clientRect.right() - width;
        y = clientRect.top();
        break;
    default:
        x = clientRect.right() - width;
        y = clientRect.top();
        break;
    }

    Popup::setGeometry(x,y,width,height);
}

void Popup::changePopupPosition(int atTheTop){
    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect clientRect = desktopWidget->availableGeometry();
    QRect popupPosition;
    switch(atTheTop){
    case 0:
        popupPosition.setX(clientRect.right() - Popup::width());
        popupPosition.setY(clientRect.bottom() - Popup::height());
        break;
    case 1:
        popupPosition.setX(clientRect.right());
        popupPosition.setY(clientRect.top());
        break;
    default:
        popupPosition.setX(clientRect.right() - Popup::width());
        popupPosition.setY(clientRect.top());
        break;
    }
    Popup::setGeometry(popupPosition);
}

void Popup::XSS(QString javascript)
{
    javascriptXSS = javascript;
}

void Popup::togglePopupStatus(){
    if(isVisible() == true){
        hide();
    }
    else{
        showNormal();
    }
}

void Popup::changePopupStatus(int open){
    if(open){
        switch(open){
        case 0:
            hide();
            visible = false;
            break;
        case 1:
            showNormal();
            visible = true;
            break;
        default:
            showNormal();
            visible = true;
            break;
        }
    }
    else{
        if(visible = !visible){
            showNormal();
        }
        else{
            hide();
        }
    }
}

void Popup::changeEvent(QEvent *e){
    QWidget::changeEvent(e);
    switch (e->type()){
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Popup::createTrayIcon(){
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
    trayIcon = new QSystemTrayIcon(this);
    QIcon icon = QIcon(":/images/weboob.png");
    trayIcon->setIcon(icon);
    trayIcon->setContextMenu(trayIconMenu);
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(clickactions(QSystemTrayIcon::ActivationReason)));
}

void Popup::clickactions(QSystemTrayIcon::ActivationReason reason){
    if(reason == QSystemTrayIcon::Trigger){
        changePopupStatus();
    }
    if(reason == QSystemTrayIcon::Context){
       this->trayIconMenu->showNormal();
    }
}

void Popup::closeEvent(QCloseEvent *event){
    hide();
    event->ignore();
}

void Popup::createActions(){
    minimizeAction = new QAction(QString("Minimize"), this);
    connect (minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));

    restoreAction = new QAction(QString("Restore"), this);
    connect (restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

    quitAction = new QAction(QString("Quit"), this);
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}

void Popup::connectObject(){
    this->ui->webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("WeboobC"), this );

    ui->webView->page()->mainFrame()->evaluateJavaScript(javascriptXSS);
}

void Popup::showMessage(QString message){
    QMessageBox msgbox;
    msgbox.setText(message);
    msgbox.exec();
}

int Popup::sumIt(int a, int b){
    return a + b;
}
